package com.jetlinks.protocol.handle;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jetlinks.protocol.message.PowerProp;
import org.springframework.stereotype.Service;
import sun.font.DelegatingShape;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author Huluw
 */
public class MyMsgHandleImpl implements MyMsgHandle{
    @Override
    public JSONObject converItems(String[] items) {
        JSONObject itemObj=new JSONObject();
        for(int i=0;i<items.length;i++){
            String[] temparr=items[i].split("=");
            itemObj.put(temparr[0],temparr.length==1?"":temparr[1]);
        }
        return itemObj;
    }

    @Override
    public Map<String,Object> getMap(JSONObject jsonObject) {
        Map<String,Object> prop = new HashMap<>();
        for (String key : jsonObject.keySet()) {
            if(PowerProp.checkSum.equals(key)||PowerProp.ProVersion.equals(key)||PowerProp.msgLen.equals(key)||
                PowerProp.msgType.equals(key)||PowerProp.msgSerial.equals(key)){
                continue;
            }else{
                switch (key){
                    case PowerProp.msgTime:
                        prop.put("52",jsonObject.getString(PowerProp.msgTime));
//                    case PowerProp.devSymbol:
//                        prop.put("201",jsonObject.getString(PowerProp.devSymbol));
//                    case PowerProp.devType:
//                        prop.put("202",jsonObject.getString(PowerProp.devType));
//                    case PowerProp.devHardVer:
//                        prop.put("203",jsonObject.getString(PowerProp.devHardVer));
//                    case PowerProp.devSoftVer:
//                        prop.put("204",jsonObject.getString(PowerProp.devSoftVer));
//                    case PowerProp.signalIntensity:
//                        prop.put("205",jsonObject.getString(PowerProp.signalIntensity));
//                    case PowerProp.simCard:
//                        prop.put("206",jsonObject.getString(PowerProp.simCard));
                    default:
                        break;
                }
            }
        }
        return prop;
    }

    @Override
    public String sendToDev(String msg) {
        JSONObject jsonObject = JSON.parseObject(msg);
        JSONArray inputs = jsonObject.getJSONArray("inputs");
       //todo inputs就是平台设备功能下发的json串，对它处理后放回给设备
        return null;
    }

    int getCRC(String str){
        byte[] bytestr = str.getBytes();
        int sum = 0;
        for(int i = 0; i < bytestr.length; i++){
            sum += bytestr[i];
        }
        return sum%256;
    }
}
